package org.pay.service.impl;

import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.pay.core.entity.MchAccountHistory;
import org.pay.core.entity.MchAccountHistoryExample;
import org.pay.core.service.IMchAccountHistoryService;
import org.pay.service.dao.mapper.MchAccountHistoryMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: yangshancheng
 * @date: 17/12/4
 * @description:
 */
@Service
public class MchAccountHistoryServiceImpl implements IMchAccountHistoryService {

    @Autowired
    private MchAccountHistoryMapper mchAccountHistoryMapper;

    public Map selectSettDailyCollect(Long mchId, String collDate, byte fundDirection, int riskDay) {
        Map param = new HashMap<>();
        param.put("mch_id", mchId);
        param.put("collDate", collDate);
        param.put("fund_direction", fundDirection);
        param.put("risk_day", riskDay);
        return mchAccountHistoryMapper.selectSettDailyCollect(param);
    }

    public void updateCompleteSett(Long mchId, String collDate, int riskDay) {
        Map param = new HashMap<>();
        param.put("mch_id", mchId);
        param.put("collDate", collDate);
        param.put("risk_day", riskDay);
        mchAccountHistoryMapper.updateCompleteSett(param);
    }

    @Override
    public List<MchAccountHistory> select(Long mchId, int offset, int limit, MchAccountHistory mchAccountHistory) {
        MchAccountHistoryExample example = new MchAccountHistoryExample();
        example.setOrderByClause("create_time DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        MchAccountHistoryExample.Criteria criteria = example.createCriteria();
        if(mchId != null) criteria.andMchIdEqualTo(mchId);
        setCriteria(criteria, mchAccountHistory);
        return mchAccountHistoryMapper.selectByExample(example);
    }

    @Override
    public int count(Long mchId, MchAccountHistory mchAccountHistory) {
        MchAccountHistoryExample example = new MchAccountHistoryExample();
        MchAccountHistoryExample.Criteria criteria = example.createCriteria();
        if(mchId != null) criteria.andMchIdEqualTo(mchId);
        setCriteria(criteria, mchAccountHistory);
        return mchAccountHistoryMapper.countByExample(example);
    }

    @Override
    public List<MchAccountHistory> select(int offset, int limit, MchAccountHistory mchAccountHistory) {
        return select(null, offset, limit, mchAccountHistory);
    }

    @Override
    public int count(MchAccountHistory mchAccountHistory) {
        return count(null, mchAccountHistory);
    }

    @Override
    public MchAccountHistory findById(Long id) {
        return mchAccountHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public MchAccountHistory findById(Long mchId, Long id) {
        MchAccountHistoryExample example = new MchAccountHistoryExample();
        MchAccountHistoryExample.Criteria criteria = example.createCriteria();
        if(mchId != null) criteria.andMchIdEqualTo(mchId);
        criteria.andIdEqualTo(id);
        List<MchAccountHistory> mchAccountHistoryList = mchAccountHistoryMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(mchAccountHistoryList)) return mchAccountHistoryList.get(0);
        return null;
    }

    void setCriteria(MchAccountHistoryExample.Criteria criteria, MchAccountHistory mchAccountHistory) {
        if(mchAccountHistory != null) {
            if(mchAccountHistory.getId() != null) criteria.andIdEqualTo(mchAccountHistory.getId());
            if(mchAccountHistory.getMchId() != null) criteria.andMchIdEqualTo(mchAccountHistory.getMchId());
            if(mchAccountHistory.getFundDirection() != null && !"-99".equals(mchAccountHistory.getFundDirection())) criteria.andFundDirectionEqualTo(mchAccountHistory.getFundDirection());
        }
    }

}
